<?php
namespace app\wypadmin\controller;
use think\Request;

/**
 * 房源类别
 */
class Fang extends Base
{	
	private $cate;

	public function _initialize(){
		parent::_initialize();
		//	获取类别模型
		$this->cate = Model('Cate/Category');
		$this->fang = Model('Fang');
	}
/**
 *  递归获取所有分类
 */
    public function menuTree($arr,$pid=0,$level=0){
        static $tree = array();
        foreach ($arr as $v) {
            if ( $v['pid'] == $pid) {
                $v['level'] = $level;
                $v['catename'] = '├ ' . str_repeat('---- ',$level) . $v['catename'];
                $tree[] = $v;
                $this->menuTree($arr,$v['id'],$level+1);
            }
        }
        return $tree;
    }
	public function add(){
		$cates 		= $this->cate->getList();
		$cates 		= $this->menuTree($cates);
		$option 	= '';

		foreach ($cates as $v) {
			$option .= '<option value="' . $v['id'] .'">' . $v['catename'] . '</option>';
		}
		
		$this->assign('webname',$this->webname);
		$this->assign('option',$option);
		return $this->fetch();
	}

	public function savefang(){
		$request = Request::instance();

		if (!$request->isPost()) {
			echo '页面不存在！';
			return;
		}

		$data['id'] 		= $request->post('id');
		$data['cateid'] 	= $request->post('cateid');
		$data['sn'] 		= $request->post('sn');
		$data['xiaoqu'] 	= $request->post('xiaoqu');
		$data['address'] 	= $request->post('address');
		$data['louceng'] 	= $request->post('louceng');
		$data['mianji'] 	= $request->post('mianji');
		$data['shoujia'] 	= $request->post('shoujia');
		$data['contact'] 	= $request->post('contact');
		$data['tel'] 		= $request->post('tel');
		$data['beizhu'] 	= $request->post('beizhu');
		$data['adddate']	= time();

		if ($data['id']) {
			$res = $this->fang->save($data,['id'=>$data['id']]);
			if ($res) {
				echo '<script>alert("更新完成");location.href="' . url('lst') . '"</script>';
			}else{
				echo '<script>alert("更新失败");history.back();"</script>';
			}
		}else{			
			$res = $this->fang->save($data);
			if ($res) {
				echo '<script>alert("添加完成");location.href="' . url('add') . '"</script>';
			}else{
				echo '<script>alert("添加失败");history.back();"</script>';
			}
		}
	}

	public function edit($id){
		$info	= $this->fang->getOne($id);
		$cates 		= $this->cate->getList();
		$cates 		= $this->menuTree($cates);
		$option 	= '';

		foreach ($cates as $v) {
			if ($info['cateid'] == $v['id']) {
				$option .= '<option value="' . $v['id'] .'" selected="selected">' . $v['catename'] . '</option>';
			}else{				
				$option .= '<option value="' . $v['id'] .'">' . $v['catename'] . '</option>';
			}
		}
		$this->assign([
			'info' 		=> $info,
			'option' 	=> $option
		]);
		$this->assign('webname',$this->webname);
		return $this->fetch();
	}

	public function lst(){
		$list = $this->fang->getList($this->pagecount);
		$page = $list->render();
		$tr = '';
		foreach ($list as $v) {
			$tr .= '
					<tr>
						<td>' . $v['id'] . '</td>
						<td>' . $this->getCateName($v['cateid']) . '</td>
						<td>' . $v['sn'] . '</td>
						<td>' . $v['xiaoqu'] . '</td>
						<td>' . $v['address'] . '</td>
						<td>' . $v['louceng'] . '</td>
						<td>' . $v['mianji'] . '</td>
						<td>' . $v['shoujia'] . '</td>
						<td>' . $v['contact'] . '</td>
						<td>' . $v['tel'] . '</td>
						<td>' . date('Y-m-d',$v['adddate']) . '</td>
						<td>
							<a href="' . url('edit','id=' .$v['id']) . '"><span class="glyphicon glyphicon-pencil"></span>编辑</a> | 
							<a href="javascript:del(' . $v['id'] . ');"><span class="glyphicon glyphicon-trash"></span>删除</a>
						</td>
					</tr>
					<tr>
						<td>备注：</td><td colspan="11">' . $v['beizhu'] . '</td>
					</tr>
					';
		}

		$this->assign('webname',$this->webname);
		$this->assign('tr',$tr);
		$this->assign('page',$page);
		$this->assign('count',count($list));
		return $this->fetch();
	}

	public function del($id){
		$where = [
			'id' => $id,
		];
		$res = $this->fang->where($where)->delete();
		if ($res) {
			echo '<script>alert("删除完成");location.href="' . url('lst') . '"</script>';
		}else{
			echo '<script>alert("删除失败");history.back();"</script>';
		}
	}

	public function getCateName($id){
		$res = $this->cate->field('catename')->find($id);
		return $res['catename'];
	}
}